package com.awei.study.dataStructure.link;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class LinkList_Abs {
    static class LinkNode {
        int value;
        LinkNode next;
        public LinkNode(int value, LinkNode next) {
            this.value = value;
            this.next=next;
        }
    }

     static class list {
        LinkNode head;

        public list(LinkNode head) {
            this.head = head;
        }

        public void addFirst(LinkNode first) {
            first.next = head;
            head = first;
        }

        public LinkNode removeFirst() {
            LinkNode first = head;
            if(head != null) {
                head = head.next;
            }
            return first;
        }
    }

    // 输出链表
    public void print(LinkNode head) {
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.value);
            head = head.next;
        }
        System.out.println(Arrays.toString(list.toArray()));
    }
    // 准备数据
    public LinkNode prepareData() {
        LinkNode l5 = new LinkNode(5,null);
        LinkNode l4 = new LinkNode(4,l5);
        LinkNode l3 = new LinkNode(3,l4);
        LinkNode l2 = new LinkNode(2,l3);
        LinkNode l1 = new LinkNode(1,l2);
        return l1;
    }

    public LinkNode prepareData(String value) {
        String[] split = value.split(",");
        LinkNode ret = new LinkNode(-1,null), re = ret;
        for (String s : split) {
//            ret = new LinkNode(Integer.parseInt(s), ret);
            ret.next = new LinkNode(Integer.parseInt(s), null);
            ret = ret.next;
        }
        return re.next;
    }

    public List<LinkNode> prepareDataList(LinkNode... arr) {
        return  Arrays.asList(arr);
    }

    public LinkNode[] prepareDataArr(LinkNode... arr) {
        return  arr;
    }

    public LinkNode prepareCycle() {
        LinkNode l7 = new LinkNode(7,null);
        LinkNode l6 = new LinkNode(6,l7);
        LinkNode l5 = new LinkNode(5,l6);
        LinkNode l4 = new LinkNode(4,l5);
        LinkNode l3 = new LinkNode(3,l4);
        LinkNode l2 = new LinkNode(2,l3);
        LinkNode l1 = new LinkNode(1,l2);

        l7.next = l4;
        return l1;
    }
}
